package club.caihappy.store.controller;

import club.caihappy.store.entity.Address;
import club.caihappy.store.service.IAddressService;
import club.caihappy.store.service.ICartService;
import club.caihappy.store.util.JsonResult;
import club.caihappy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by DELL on 2022/3/16
 **/
@RestController
@RequestMapping("carts")
public class CartController extends BaseController {

    @Autowired
    private ICartService cartService;

    @RequestMapping("add_to_cart")
    public JsonResult<Void> addToCart(Integer pid, Integer num, HttpSession session) {
        cartService.addToCart(pid, getUidFromSession(session), num, getUsernameFromSession(session));
        return new JsonResult<>(OK, "添加购物车成功");
    }

    @RequestMapping({"", "/"})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session) {
        List<CartVO> data = cartService.getVOByUid(getUidFromSession(session));
        return new JsonResult<>(OK, data);
    }

    @RequestMapping("list")
    public JsonResult<List<CartVO>> getVOByCid(Integer[] cids, HttpSession session) {
        List<CartVO> data = cartService.getVOByCid(getUidFromSession(session), cids);
        return new JsonResult<>(OK, data);
    }

    @RequestMapping("{cid}/{num}/change_num")
    public JsonResult<Void> changeNum(@PathVariable("cid") Integer cid, @PathVariable("num") Integer num, HttpSession session) {
        cartService.changeNum(cid, getUidFromSession(session), getUsernameFromSession(session), num);
        return new JsonResult<>(OK, "改变购物车数据成功");
    }

    @RequestMapping("delete_cart")
    public JsonResult<Void> deleteCart(Integer cid, HttpSession session) {
        cartService.deleteCart(cid, getUidFromSession(session));
        return new JsonResult<>(OK, "删除购物车数据成功");
    }

}
